// --- 辅助映射 ---
const STATUS_MAP = {
    IN_TREATMENT: { name: '就诊中', class: 'status-active' },
    NEED_REVIEW: { name: '需复诊', class: 'status-review' },
    FOLLOW_UP: { name: '长期跟进', class: 'status-follow' },
    ARCHIVED: { name: '档案归档', class: 'status-archived' },
};

/**
 * 封装单个患者档案详情的 Mock 数据
 */
const getPatientProfileDetailMockData = (patientId) => {
  // 实际业务中，根据 patientId 调用 API 获取数据
  return {
    status: 'IN_TREATMENT', // 档案状态
    statusName: STATUS_MAP['IN_TREATMENT'].name,
    statusClass: STATUS_MAP['IN_TREATMENT'].class,

    base: {
      id: patientId,
      name: '李思涵',
      gender: '女',
      age: 25,
      phone: '138****8888',
      doctor: '魏医生',
      initialVisitDate: '2025-05-01',
      nextReviewDate: '2025-12-15',
      source: '转介绍',
      remark: '隐适美项目，患者非常配合，自述戴满22小时。',
    },
    stats: {
      totalVisits: 15,
      totalFee: '7.2', // 万
      projectCount: 2, // 进行中的项目数
      lastVisitDate: '2025-11-20',
      imageCount: 24,
    },
    // 关键：这里是该患者的所有病例列表 (原病例管理信息)
    records: [ 
      { recordId: 'REC0015', date: '25-11-20', project: '隐适美复诊 (15/40)', summary: '更换牙套，附件检查，费用 120。', status: '已完成', statusClass: 'finish' },
      { recordId: 'REC0014', date: '25-10-25', project: '牙结石清理', summary: '深度洁牙，牙周检查，无异常。', status: '已完成', statusClass: 'finish' },
      { recordId: 'REC0013', date: '25-09-10', project: '矫正方案确认', summary: '方案讲解，合同签订，启动治疗。', status: '已完成', statusClass: 'finish' },
      { recordId: 'REC0001', date: '25-05-01', project: '初诊检查', summary: '口腔全景片拍摄，制定初诊报告。', status: '已完成', statusClass: 'finish' },
    ],
    imageSamples: [
      'https://picsum.photos/id/101/100/100',
      'https://picsum.photos/id/102/100/100',
      'https://picsum.photos/id/103/100/100',
      'https://picsum.photos/id/104/100/100',
      'https://picsum.photos/id/105/100/100',
      'https://picsum.photos/id/106/100/100',
    ]
  };
};


Page({
  data: {
    profile: {},
    currentTab: 'records', // 默认显示病例
    patientId: null,
  },

  onLoad(options) {
    const id = options.id || 'P00123'; 
    this.setData({ patientId: id });
    this.fetchProfileData(id);
  },

  fetchProfileData(id) {
    // 实际业务中：调用 API 获取该患者的档案和所有病例列表
    const data = getPatientProfileDetailMockData(id);
    this.setData({ profile: data });
  },

  // Tab 切换
  switchTab(e) {
    this.setData({ currentTab: e.currentTarget.dataset.tab });
  },

  // 快捷操作：电话联系
  callPatient() {
    wx.makePhoneCall({ phoneNumber: this.data.profile.base.phone });
  },

  // 快捷操作：新建诊疗记录
  createNewRecord() {
    // 跳转到创建诊疗记录页，并带上患者 ID
    wx.navigateTo({ 
      url: `/pages/doctor/treatment-record/treatment-record?patientId=${this.data.patientId}` 
    });
  },

  // 查看病例详情 (跳转到病例管理页)
  viewCaseDetail(e) {
    const recordId = e.currentTarget.dataset.id;
    // 问题 1 解决：跳转到 Case Management 时，我们带上 patientId 和 recordId
    wx.navigateTo({
      url: `/pages/case-management/case-management?patientId=${this.data.patientId}&recordId=${recordId}`
    });
  },
  
  // 预览影像
  previewImage(e) {
    const current = e.currentTarget.dataset.src;
    wx.previewImage({
      current: current,
      urls: this.data.profile.imageSamples 
    });
  }
});